package com.glaivelee.test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.glaivelee.abstracts.chooseType;

public class Pattern7 extends chooseType {
	private static Iterator<String> itr;
	private List<String> unshellList = new ArrayList<String>();

	public Pattern7(String srcValue) {
		super(srcValue);
		itr = regexList.iterator();
	}

	public static void main(String[] args) {
		dataInit();
		itr = regexList.iterator();
		String s = "--> 中关村   ZGQ <--<div style=\"helloworld\"> "
				+ "<table>mydatatable</table> -->&nbsp;<--  "
				+ "<p >p1start <p >p2 </p>first p last</p>"
				+ " &nbsp;   -->div-start    &nbsp;&nbsp;<--  </div>first div end";
		// unshell();
		TreeSet<String> treelistPrefix = new TreeSet<String>();
		TreeSet<String> treelistPostfix = new TreeSet<String>();

		while (itr.hasNext()) {
			String positivelookahead = getPositivelookahead(itr.next());
			System.out.println("positivelookahead:" + positivelookahead);
			 Pattern p = Pattern.compile(positivelookahead);
			 Matcher m = p.matcher(s);
			 if (m.find()) {
				 String prefix = m.group("prefixLabel");
				 String postfix = m.group("postfixLabel");
				 String style = m.group("style");
				 String content = m.group("content");
				 treelistPrefix.add(prefix);
				 treelistPostfix.add(postfix);
				 System.out.println("prefix:" + prefix);
				 System.out.println("postfix:" + postfix);
				 System.out.println("style:" + style);
				 System.out.println("content:" + content);
				 
			 }
			 System.out.println();
		}
		 System.out.println("treelistPrefix-pollFirst:" + treelistPrefix.pollFirst());
		 System.out.println("treelistPostfix-pollFirst:" + treelistPostfix.pollFirst());
	}

	public static String getPositivelookahead(String regexList) {
		// "(?<prefixLabel>(.*?))(?="+regexList+")";
		return "(?<prefixLabel>(.*?))(" + regexList + ")(?<postfixLabel>(.*?))";
	}

	public static String getPositivelookahead2() {
		// "(?<prefixLabel>(.*?))(?="+regexList+")";
		StringBuilder sb = new StringBuilder();
		sb.append("(?<prefixLabel>(.*?))(?=");
		sb.append("[");
		while (itr.hasNext()) {
			sb.append("(" + itr.next() + ")");
		}
		sb.deleteCharAt(sb.length() - 1);
		sb.append("]");
		sb.append(")");
		return sb.toString();
	}

	public static void test() {
		String[] s = { "<div style=\"color:red\" > &nbsp;  </div>",
				"<div style=\"color:red\" ><p></p> &nbsp;helloworld  </div>" };
		Pattern p = Pattern.compile("<div(?<style>(.*?))>(?<div>(.*?))</div>");
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < s.length; i++) {
			Matcher m = p.matcher(s[i]);
			while (m.find()) {
				// list.add(i + " : " + m.group("div"));
				System.out.println(i + "style-->" + m.group("style") + "<--");
				System.out.println(i + "div-->" + m.group("div") + "<--");
			}
		}
	}

	private void unshell(String ssc) {
		String contentOfPattern = getContentOfPattern(ssc);
		if (hasLabel(contentOfPattern)) {
			unshell(contentOfPattern);
		} else {
			unshellList.add(contentOfPattern);
		}
	}

	// public static String clearHtmlLabels(String groupStr) {
	// while (itr.hasNext()) {
	// String nextRegex = itr.next();
	// Pattern p = Pattern.compile(nextRegex, 1);
	// Matcher m = p.matcher(groupStr);
	// groupStr = m.replaceAll("").trim();
	// }
	// return ClearSpace(groupStr);
	// }

	private boolean hasLabel(String ssc) {
		while (itr.hasNext()) {
			String s = itr.next();
			Pattern p = Pattern.compile(s);
			Matcher m = p.matcher(ssc);
			if (m.find()) {
				return true;
			}
		}
		return false;
	}

	public static String getContentOfPattern(String groupStr) {
		while (itr.hasNext()) {
			String nextRegex = itr.next();
			Pattern p = Pattern.compile(nextRegex, 1);
			Matcher m = p.matcher(groupStr);
			if (m.find()) {
				return ClearSpace(m.group("content"));
			}
		}
		return ClearSpace(groupStr);
	}
}
